package com.gitee.wsl.ext.collection


/**
 * Like [singleOrNull] but the single item must be of type [T].
 */
inline fun <reified T> Collection<*>.singleOfTypeOrNull(predicate: (T) -> Boolean = { true }): T? {
    return singleOrNull { it is T && predicate(it) } as T?
}

/**
 * Like [singleOrNull] but the single item must be of type [T].
 */
inline fun <reified T> Collection<*>.singleOfType(predicate: (T) -> Boolean = { true }): T {
    return singleOfTypeOrNull(predicate)!!
}

/**
 * Like [singleOrNull] but the single item must be of type [T].
 */
inline fun <reified T> Sequence<*>.singleOfTypeOrNull(predicate: (T) -> Boolean = { true }): T? {
    return singleOrNull { it is T && predicate(it) } as T?
}

/**
 * Like [singleOrNull] but the single item must be of type [T].
 */
inline fun <reified T> Sequence<*>.singleOfType(predicate: (T) -> Boolean = { true }): T {
    return singleOfTypeOrNull(predicate)!!
}